<?php
/**
 * Author: liujin
 * Date: 2022/3/15
 * Time: 6:07 下午
 * Describe:
 */

namespace app\index\controller;


class Uploadfile extends Base {


    //上传图片接口
    public function upload()
    {
        $file = request()->file('file');
        if (empty($file)) {
            $res['success'] = false;
            $res['desc'] = '文件参数不能为空';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $info = $file->validate(['ext' => 'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images');
        if ($info) {
            // 成功上传后 获取上传信息
            $url = 'uploads/images' . DIRECTORY_SEPARATOR . $info->getSaveName();
            $url = str_replace(DIRECTORY_SEPARATOR, '/', $url);
        } else {
            $res['success'] = false;
            $res['desc'] = $file->getError();
            $res['errorcode'] = '200022';
            return json($res);
        }
        $res['success'] = true;
        $res['data']['url'] = get_http_type() . $_SERVER['HTTP_HOST'] . DIRECTORY_SEPARATOR. $url;
        $res['desc'] = '上传成功';
        $res['errorcode'] = '';
        return json($res);

    }

    //上传文件接口
    public function uploadFile()
    {
        $file = request()->file('file');
        if (empty($file)) {
            $res['success'] = false;
            $res['desc'] = '文件参数不能为空';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $info = $file->validate(['ext' => 'xls,xlsx'])->move(ROOT_PATH . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'excel');
        if ($info) {
            // 成功上传后 获取上传信息
            $url = 'uploads/excel' . DIRECTORY_SEPARATOR . $info->getSaveName();
            $url = str_replace(DIRECTORY_SEPARATOR, '/', $url);
        } else {
            $res['success'] = false;
            $res['desc'] = '请， 使用正确的文件， 后缀为：xls, xlsx，csv 的文件 ' . $file->getError();
            $res['errorcode'] = '200022';
            return json($res);
        }
        $res['success'] = true;
        $res['data']['url'] = $url;
        $res['desc'] = '上传成功';
        $res['errorcode'] = '';
        return json($res);

    }

}
